package com.csii.mobilebanking.user;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.csii.mobilebanking.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * 自定义的xpandableList Adapter 根据传过来的值 来动态添加功能 1 数组 2 list
 * 
 * @author Administrator
 * 
 */
public class ExpListAd extends BaseExpandableListAdapter {
	// 分组数据-数组
	private String[] group = { "A组", "B组", "C组", "D组" };
	private String[][] child = { { "A01", "A02", "A03" },
			{ "B01", "B02", "B03" }, { "C01", "C02", "C03" },
			{ "D04", "D05", "D06" } };
	// 分组数据-list
	private List<String> groupArray;
	private List<List<String>> childArray;
	private Context mContext;
	   //定义构建Adpater的组要用的组数据.  
    List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();  
    //定义构建Adpater的子条目要用的组数据.  
    List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();  
    //LinkedHashMap会根据添加时顺序存放数据.  
    Map<Map<String, String>, List<Map<String, String>>> tempMap = new LinkedHashMap<Map<String, String>, List<Map<String, String>>>(); 
	public ExpListAd(Context context) {
		this.mContext = context;
		initData();
	}

	// public ExpListAd(Context context, List<String> groupArray,
	// List<List<String>> childArray) {
	//
	// initData();
	// }

	private void initData() {
		groupArray = new ArrayList<String>();
		childArray = new ArrayList<List<String>>();

		groupArray.add("第一组");
		groupArray.add("第二组");

		List<String> tempArray = new ArrayList<String>();
		tempArray.add("第1条");
		tempArray.add("第2条");
		tempArray.add("第3条");

		for (int index = 0; index < groupArray.size(); ++index) {
			childArray.add(tempArray);
		}
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return childArray.get(groupPosition).get(childPosition);
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.exp_child, null);
			// LinearLayout layout = (LinearLayout)
			// LayoutInflater.from(mContext)
			// .inflate(R.layout.exp_child, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.ctv);
		tv.setText(getChild(groupPosition, childPosition).toString());
		return view;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childArray.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return groupArray.get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		return groupArray.size();
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = inflater.inflate(R.layout.exp_group, null);
			// LinearLayout layout = (LinearLayout)
			// LayoutInflater.from(mContext)
			// .inflate(R.layout.exp_group, null);
		}
		TextView tv = (TextView) view.findViewById(R.id.gtv);
		tv.setText(getGroup(groupPosition).toString());
		// 判断分组是否展开，分别传入不同的图片资源
		if (isExpanded) {
			// todo
		} else {
			// todo
		}
		return view;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

}
